package com.jmcgg.freesky.common.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;


/**
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company: Amei's FreeSky Studio
 * </p>
 * <p>
 * Sql Server��ݿⱸ�ݴ���
 * </p>
 * 
 * @author <b>Jacky Chou</b> (Copyright &#169; 2005~2010 AFSS [Amei's FreeSky
 *         Studio])
 * @version 1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2 <br>
 *          1.0.0, 2006-05-30, Initial Version
 */

public class DBOperator {
	/**
	 * Log4J Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DBOperator.class);

	public DBOperator() {
	}

	// 1 ��ʾ�ɹ�, 0 ��ʾʧ�� fileName����·��

	public int backupAll(String databaseName, String fileName) {
		delBackupDevice();

		int iReturn = 0;
		String strSQL = "";
		Connection conn;
		Statement statement = null;
		DBConnBean conndb = new DBSql2kBean();
		conn = conndb.getDBConnection();
		try {
			conn.setAutoCommit(false);
			statement = conn.createStatement();

			strSQL = "USE master";
			statement.executeUpdate(strSQL);

			strSQL = "EXEC sp_addumpdevice 'disk', 'backup_device', '"
					+ fileName + "'";
			statement.executeUpdate(strSQL);

			strSQL = "BACKUP DATABASE " + databaseName + " TO backup_device";
			statement.executeUpdate(strSQL);

			strSQL = "sp_dropdevice 'backup_device'";
			statement.executeUpdate(strSQL);
			conn.commit();
			iReturn = 1;
		} catch (SQLException ex) {
			logger.error("backupAll SQLException: " + ex.getMessage());
			// System.err.println("backupAll SQLException: " + ex.getMessage());
			try {
				conn.rollback();
			} catch (Exception e) {
				logger.error("Rollback error" + e.getMessage());
			}

		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
				if (conn != null) {
					conn.close();
				}
				conndb.close();
			} catch (Exception e) {
				logger.error("addLogRecord exception: " + e.getMessage());
			}
		}

		return iReturn;
	}

	public int restoreAll(String databaseName, String fileName) {
		delBackupDevice();

		int iReturn = 0;
		String strSQL = "";

		Connection conn;
		Statement statement = null;
		DBConnBean conndb = new DBSql2kBean();
		conn = conndb.getDBConnection();
		try {
			conn.setAutoCommit(false);
			statement = conn.createStatement();
			strSQL = "USE master";
			statement.executeUpdate(strSQL);

			strSQL = "EXEC sp_addumpdevice 'disk', 'backup_device', '"
					+ fileName + "'";
			statement.executeUpdate(strSQL);

			strSQL = "RESTORE DATABASE " + databaseName
					+ " FROM backup_device WITH REPLACE";
			statement.executeUpdate(strSQL);

			strSQL = "sp_dropdevice 'backup_device'";
			statement.executeUpdate(strSQL);
			conn.commit();
			iReturn = 1;
		} catch (SQLException ex) {
			logger.error("restoreAll SQLException: " + ex.getMessage());
			try {
				conn.rollback();
			} catch (Exception e) {
				logger.error("Rollback error" + e.toString());
			}
		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
				if (conn != null) {
					conn.close();
				}
				conndb.close();
			} catch (Exception e) {
				logger.error("addLogRecord exception:" + e.toString());
			}
		}

		return iReturn;
	}

	public void delBackupDevice() {
		String strSQL = "";
		Connection conn;
		Statement statement = null;
		DBConnBean conndb = new DBSql2kBean();
		conn = conndb.getDBConnection();
		try {
			conn.setAutoCommit(false);
			statement = conn.createStatement();

			strSQL = "USE master";
			statement.executeUpdate(strSQL);

			strSQL = "sp_dropdevice 'backup_device'";
			statement.executeUpdate(strSQL);
			conn.commit();
		} catch (SQLException ex) {
			logger.error("delBackupDevice SQLException: " + ex.getMessage());
			try {
				conn.rollback();
			} catch (Exception e) {
				logger.error("Rollback error" + e.toString());
			}

		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
				if (conn != null) {
					conn.close();
				}
				conndb.close();
			} catch (Exception e) {
				logger.error("delBackupDevice exception:" + e.toString());
			}
		}
	}

}
